package com.ruoyi.tongban.service.impl;

import java.io.ByteArrayInputStream;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tongban.utils.QiniuCloudUtil;
import com.ruoyi.tongban.utils.WechatApi;
import com.ruoyi.tongban.utils.WechatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tongban.mapper.AppletsQrCodeMapper;
import com.ruoyi.tongban.domain.AppletsQrCode;
import com.ruoyi.tongban.service.IAppletsQrCodeService;

/**
 * 小程序二维码Service业务层处理
 * 
 * @author caiyanshan
 * @date 2021-03-10
 */
@Service
public class AppletsQrCodeServiceImpl implements IAppletsQrCodeService 
{
    @Autowired
    private AppletsQrCodeMapper appletsQrCodeMapper;

    /**
     * 查询小程序二维码
     * 
     * @param appletsQrcodeId 小程序二维码ID
     * @return 小程序二维码
     */
    @Override
    public AppletsQrCode selectAppletsQrCodeById(Long appletsQrcodeId)
    {
        return appletsQrCodeMapper.selectAppletsQrCodeById(appletsQrcodeId);
    }
    
    /**
     * @Description 按照机构ID和班级ID查询小程序二维码
     * @Author caiyanshan
     * @Date 2021/3/10 13:55
     * @Param []
     * @Return com.ruoyi.tongban.domain.AppletsQrCode
     */
    public List<AppletsQrCode> getQrCodeByInstitutionId(Long institionId){
        return this.getQrCodeByInstitutionId(institionId, null);
    }
    
    /**
     * @Description 按照机构ID查询小程序二维码
     * @Author caiyanshan
     * @Date 2021/3/10 13:55
     * @Param []
     * @Return com.ruoyi.tongban.domain.AppletsQrCode
     */
    public List<AppletsQrCode> getQrCodeByInstitutionId(Long institionId, Long classId){
        AppletsQrCode appletsQrCode = new AppletsQrCode();
        appletsQrCode.setInstitutionId(institionId);
        appletsQrCode.setClassId(classId);
        List<AppletsQrCode> appletsQrCodes = appletsQrCodeMapper.selectAppletsQrCodeList(appletsQrCode);
        if(appletsQrCodes.size() == 0){//没有则调接口获取
            byte[] bytes = WechatApi.getQRImage(institionId, classId);
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            JSONObject jsonResult = QiniuCloudUtil.upload(in,"applets-tongban/qrcode/" + System.currentTimeMillis() + ".jpg");
            String qrCodeUrl = jsonResult.get("key").toString();
            appletsQrCode.setQrcodeUrl(qrCodeUrl);
            appletsQrCode.setCreateTime(DateUtils.getNowDate());
            appletsQrCodeMapper.insertAppletsQrCode(appletsQrCode);//插入数据库
            appletsQrCodes.add(appletsQrCode);
            return appletsQrCodes;
        }
        return appletsQrCodes;
    }

    /**
     * 查询小程序二维码列表
     * 
     * @param appletsQrCode 小程序二维码
     * @return 小程序二维码
     */
    @Override
    public List<AppletsQrCode> selectAppletsQrCodeList(AppletsQrCode appletsQrCode)
    {
        return appletsQrCodeMapper.selectAppletsQrCodeList(appletsQrCode);
    }

    /**
     * 新增小程序二维码
     * 
     * @param appletsQrCode 小程序二维码
     * @return 结果
     */
    @Override
    public int insertAppletsQrCode(AppletsQrCode appletsQrCode)
    {
        appletsQrCode.setCreateTime(DateUtils.getNowDate());
        return appletsQrCodeMapper.insertAppletsQrCode(appletsQrCode);
    }

    /**
     * 修改小程序二维码
     * 
     * @param appletsQrCode 小程序二维码
     * @return 结果
     */
    @Override
    public int updateAppletsQrCode(AppletsQrCode appletsQrCode)
    {
        return appletsQrCodeMapper.updateAppletsQrCode(appletsQrCode);
    }

    /**
     * 批量删除小程序二维码
     * 
     * @param appletsQrcodeIds 需要删除的小程序二维码ID
     * @return 结果
     */
    @Override
    public int deleteAppletsQrCodeByIds(Long[] appletsQrcodeIds)
    {
        return appletsQrCodeMapper.deleteAppletsQrCodeByIds(appletsQrcodeIds);
    }

    /**
     * 删除小程序二维码信息
     * 
     * @param appletsQrcodeId 小程序二维码ID
     * @return 结果
     */
    @Override
    public int deleteAppletsQrCodeById(Long appletsQrcodeId)
    {
        return appletsQrCodeMapper.deleteAppletsQrCodeById(appletsQrcodeId);
    }
}
